/**
The MIT License (MIT)

Copyright (c) 2017 simpleplus

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.oschina.simpleplus.ellieproducer;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.oschina.simpleplus.ellie.ServiceDiscovery;
import net.oschina.simpleplus.ellieapi.Request;

public class ClusterNettyProducer implements Producer {
	
	private ServiceDiscovery sd;
	private ClusterProducerPool pool = null;
		
	//private static final Logger log = LoggerFactory.getLogger(ClusterNettyProducer.class);

	public ClusterNettyProducer(String zooKeeperAddr, GenericObjectPoolConfig cnf) throws Exception{
		super();
		sd = new ServiceDiscovery();
		sd.connetAndWatchNode(zooKeeperAddr);
		pool = new ClusterProducerPool(new PooledObjectFactory<NettyProducer>() {

			@Override
			public PooledObject<NettyProducer> makeObject() throws Exception {
				String discover = sd.discover();
				//log.info(discover);
				return new DefaultPooledObject<>(new NettyProducer(discover));
			}

			@Override
			public void destroyObject(PooledObject<NettyProducer> p) throws Exception {
				p.getObject().shutdown();
			}

			@Override
			public boolean validateObject(PooledObject<NettyProducer> p) {
				
				return p.getObject().validate();
			}

			@Override
			public void activateObject(PooledObject<NettyProducer> p) throws Exception {
				
			}

			@Override
			public void passivateObject(PooledObject<NettyProducer> p) throws Exception {
				
			}
		}, cnf);
		
		pool.preparePool();
	}


	@Override
	public void send(Request request, ProducerCallBack pcb) {
		NettyProducer np = null;
		try {
			np = pool.borrowObject();
			//log.info(np.toString());
			np.send(request, pcb);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (np != null)
				pool.returnObject(np);
		}
	}

	@Override
	public void shutdown() {
		pool.clear();
		pool.close();
	}
	
	
	private static class ClusterProducerPool extends GenericObjectPool<NettyProducer> {

		public ClusterProducerPool(PooledObjectFactory factory, GenericObjectPoolConfig cnf) {
			super(factory, cnf);
		}
		
	}

}
